package com.ggh.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.ggh.config.entity.JdbcConfig;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;
/**
 * @author ： jsq
 * @date ：2019/10/12 23:27
 * @description : 数据库连接池，管理连接队列
 * @path : com.ggh.config.DataSourceConfig
 * @modifiedBy ：
 */
@Configuration
@MapperScan("com.ggh.dao")
@Slf4j
public class DataSourceConfig {
    @Autowired
    private JdbcConfig jdbcConfig;
    /**
    * @author : jsq
    * @date : 2019/10/13 0:18
    * @description :在同样的DataSource中，首先使用被标注的DataSource
    * @return javax.sql.DataSource
    * @throws
    */

    @Bean
    @Primary
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(jdbcConfig.getUrl());
        druidDataSource.setUsername(jdbcConfig.getUserName());
        druidDataSource.setPassword(jdbcConfig.getPassword());
        druidDataSource.setInitialSize(jdbcConfig.getInitialSize());
        druidDataSource.setMinIdle(jdbcConfig.getMinIdle());
        druidDataSource.setMaxActive(jdbcConfig.getMaxActive());
        druidDataSource.setTimeBetweenEvictionRunsMillis(jdbcConfig.getTimeBetweenEvictionRunsMillis());
        druidDataSource.setMinEvictableIdleTimeMillis(jdbcConfig.getMinEvictableIdleTimeMillis());
        druidDataSource.setValidationQuery(jdbcConfig.getValidationQuery());
        druidDataSource.setTestWhileIdle(jdbcConfig.isTestWhileIdle());
        druidDataSource.setTestOnBorrow(jdbcConfig.isTestOnBorrow());
        druidDataSource.setTestOnReturn(jdbcConfig.isTestOnReturn());
        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(jdbcConfig.getMaxPoolPreparedStatementPerConnectionSize());
        try {
            druidDataSource.setFilters(jdbcConfig.getFilters());
        } catch (SQLException e) {
            if (log.isInfoEnabled()) {
                log.info(e.getMessage(), e);
            }
        }
        return druidDataSource;
    }
    /**
     * @author : jsq
     * @date : 2019/10/13 0:01
     * @description : 配置事物管理器
     */
    @Bean(name="transactionManager")
    public DataSourceTransactionManager transactionManager(){
        return new DataSourceTransactionManager(dataSource());
    }
    /**
    * @author : jsq
    * @date : 2019/10/13 0:01
    * @description : 配置druid监听图像化

    * @return org.springframework.boot.web.servlet.ServletRegistrationBean
    * @throws
    */

    @Bean
    public ServletRegistrationBean startViewServlet(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet (),"/druid/*");
        // IP白名单
        servletRegistrationBean.addInitParameter("allow",jdbcConfig.getAllow ());
        // IP黑名单(共同存在时，deny优先于allow)
        //servletRegistrationBean.addInitParameter("deny","127.0.0.1");
        //控制台管理用户
        servletRegistrationBean.addInitParameter("loginUsername",jdbcConfig.getLoginUsername ());
        servletRegistrationBean.addInitParameter("loginPassword",jdbcConfig.getLoginPassword ());
        //是否能够重置数据
        servletRegistrationBean.addInitParameter("resetEnable",jdbcConfig.getResetEnable ());
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean statFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter ());
        //添加过滤规则
        filterRegistrationBean.addUrlPatterns("/*");
        //忽略过滤的格式
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}
